<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://www.ufida.com/multilang" prefix="ml" %>

TreeViewComp.prototype.onBeforeNodeCreate = function(row) {
	if(this.id == "advanceTree"){
		var fieldType = row.getCellValue(9);
		var logicType = row.getCellValue(14);
		if(fieldType == 0 || fieldType == 1 || logicType == 1)
			return false;
	}
    return true;
};

function OnRootNodeCreatedFun(node){
	node.changeCaption("${ml:transp('lfw', 'query_allcondition')}");
}
function gerChangeNodeFun(node){
	node.changeCaption("${ml:transp('lfw' , 'query_generalquery')}");
}

/**
 *Init
 */
function externalInit() {
	window.$c_normalPanel = new QueryTemplatePanel(document.getElementById("$d_normalPanel"), "normalPanel", 0, 0, "100%", "100%", "relative");
    $c_normalPanel.setDataset(getDataset("queryConditionDataset"));
    window.$queryTemplateProcessor = new QueryTemplateProcessor();
    
    
	var tab = getComponent('sqlTab');
	tab.beforeItemInit = function(item){
	};
	
	tab.afterItemInit = function(item){
		window.$c_advancedPanel = new QueryTemplatePanel(document.getElementById("$d_advancePanel"), "advancePanel", 0, 0, "100%", "100%", "relative");
    	window.$c_advancedPanel.setIfMust(true);
    	$c_advancedPanel.setDataset(getDataset("queryConditionTreeDataset"));
	};
	
	var div = $ce("DIV");
	div.style.width = "65";
	var imgAttr = {position:'relative', boolFloatLeft:true};
	var imageSave = new ImageComp(div, "imageSave", window.baseGlobalPath + "/html/themes/" + window.themeId + "/images/querytemplate/save.png", 0, 2, "16", "16", trans("ml_save"), null, imgAttr); 
	var imageClear = new ImageComp(div, "imageClear", window.baseGlobalPath + "/html/themes/" + window.themeId + "/images/querytemplate/clear.png", 5, 2, "16", "16",  trans("ml_clear"), null, imgAttr); 
	var imageReset = new ImageComp(div, "imageReset", window.baseGlobalPath + "/html/themes/" + window.themeId + "/images/querytemplate/reset.png", 10, 2, "16", "16",  trans("ml_reset"), null, imgAttr); 
	tab.rightBarSpace.appendChild(div);
	imageSave.onclick = function() {
		var dialog = getComponent("saveDialog");
		dialog.show();
		doConditionSave.flag = 'Favorites';
	}
				
	imageClear.onclick = function() {
		var conditionDs = null;
		if(getComponent('sqlTab').getSelectedIndex() == 0)
			conditionDs = getDataset("queryConditionDataset");
		else
			conditionDs = getDataset("queryConditionTreeDataset");
		var rows = conditionDs.getRows();
		if(rows != null){
			for(var i = 0; i < rows.length; i ++){
				//略过固定
				if(rows[i].getCellValue(9) == 1)
					continue;
				if(rows[i].getCellValue(1) == '$#$')
					continue;
				conditionDs.setValueAt(i, 3, null);
			}
		}
	}
		
	imageReset.onclick = function() {
		var conditionDs = null;
		if(getComponent('sqlTab').getSelectedIndex() == 0)
			conditionDs = getDataset("queryConditionDataset");
		else
			conditionDs = getDataset("queryConditionTreeDataset");
		conditionDs.undo();
	}
				
	/*收藏夹*/
	 var imageFolder = new ImageComp(document.getElementById("favoritDiv"), "imageFolder", window.baseGlobalPath + "/html/themes/" + window.themeId + "/images/querytemplate/folder.png",100, 2, "16", "16",  trans("ml_folder"), null, imgAttr);
	 var imageDelete = new ImageComp(document.getElementById("favoritDiv"), "imageDelete", window.baseGlobalPath + "/html/themes/" + window.themeId + "/images/querytemplate/delete.png",105, 2, "16", "16",  trans("ml_delete"), null, imgAttr);
	 //生成文件夹
	 imageFolder.onclick = function(){
	 	var dialog = getComponent("saveDialog");
		dialog.show();
		doConditionSave.flag = 'Folder';
	 }
	 //删除
	 imageDelete.onclick = function(){
	 	deleteFavorit();
	 }
}
			
function getEntityAttrs(node) {
	/*
	if(node.isLeaf || node.childrenTreeNodes.length > 0)
		return;
	var parentRow = node.nodeData;
	var entityName = parentRow.getCellValue(5);
	var entityAttrs = getService("favorite_service").getEntityAttrs(entityName, window.level ++);
	var dsResult = entityAttrs.map.dsResult;
	var combs = entityAttrs.map.combs;
	var ds = getDataset("queryDataset");
	for(var i = 0; i < dsResult.length; i ++){
		var row = new DatasetRow();
		for(var j = 0; j < 7; j ++){
			row.setCellValue(j, dsResult[i][j]);
		}
		
		//set parent field key
		row.setCellValue(7, parentRow.getCellValue(0));
		//set parentField
		row.setCellValue(9, parentRow.getCellValue(3));
		//set table name
		row.setCellValue(10, dsResult[i][7]);
		ds.addRow(row);
	}
	
	for(var i = 0; i < combs.length; i ++)
	{
		var id = combs[i].id;
		if(getComboData(id) != null){
			continue;
		}
		var comb = new ComboData();
		for(var j = 0; j < combs[i].allCombItems.length; j ++){
			var item = combs[i].allCombItems[j];
			comb.addItem(new ComboItem(item.i18nName, item.value));
		}
		window["$cb_" + id] = comb;
	}
	*/
}

/**
 * 收藏夹保存
 */
function doConditionSave()
{  
	var saveDs = getDataset("savedQueryCondition");
	var saveName = getComponent("saveText").getValue();
	if(saveName == ''){
		showMessageDialog("${ml:trans('yer_savefoldernotempty')}");
		return;
	}
	var row;
	if(doConditionSave.flag == 'Favorites'){
		var tabIndex = getComponent('conditionTab').getSelectedIndex();
    	if(tabIndex == 0)
    		getComponent('conditionTab').activeTab(1);
	
		row = getFavoritesData(saveDs);
		if(row == null)
			return;
		var rowArray = $saveFavoritesImpl(row);
		if(rowArray == null)
			return;
		for(var i=0; i < rowArray.length; i++)
			saveDs.addRow(rowArray[i]);
	}
	else if(doConditionSave.flag == 'Folder'){
		row = getFolderData(saveDs);
		if(row == null) 
			return;
	 	var page_fun_code = getParameter("otherPageId");
		var nodeInfo = row.contentToXml() + "," + page_fun_code + "," + getSessionAttribute("templateid");//TODO
		getService("favorite_service").saveQueryCondition(null,nodeInfo);
	 	saveDs.addRow(row);
	}
	else if(doConditionSave.flag == 'modify'){
		row = doConditionSave.row;
		row.setCellValue(3,saveName);
		var page_fun_code = getParameter("otherPageId");
		var nodeInfo = row.contentToXml() + "," + page_fun_code + "," + getSessionAttribute("templateid");//TODO
		getService("favorite_service").modifyQueryConditionName(nodeInfo);
		saveDs.setValueAt(saveDs.getRowIndex(row),3,saveName);
	}
	getComponent("saveDialog").close();
	delete(doConditionSave.flag);
	delete(doConditionSave.row);
}
			
/**
 *要保存或修改的Favorites数据
 */
function getFavoritesData(saveDs)
{
	var saveName = getComponent("saveText").getValue();
	if($isNameExist()) 
		return null;
	var row = new DatasetRow();
	var guid = (Math.random()*1000000).toString().substring(0, 6);
	row.setCellValue(0, guid);
	row.setCellValue(1, guid);
	row.setCellValue(3, saveName);
	row.setCellValue(4, 'Favorites');
	row.setCellValue(5, null);
	row.setCellValue(6, 'N');
	
	var selectRow = saveDs.getSelectedRow();
 	if(selectRow != null && selectRow.getCellValue(saveDs.nameToIndex("kind")) == 'Folder'){
 		row.setCellValue(2, selectRow.getCellValue(saveDs.nameToIndex("id")));//have parent
 	}else{
 		row.setCellValue(2, null);
 	}
 	return row;
}

/**
 *要保存或修改的Folder数据
 */
function getFolderData(saveDs)
{
	var saveName = getComponent("saveText").getValue();
	if($isNameExist()) 
		return null;
 	var row = new DatasetRow();
 	var guid = (Math.random()*1000000).toString().substring(0, 6);
 	row.setCellValue(0, guid);
 	row.setCellValue(1, guid);
	row.setCellValue(3, saveName);
	row.setCellValue(4, 'Folder');
	row.setCellValue(5, null);
	row.setCellValue(6, 'N');
	
	var selectRow = saveDs.getSelectedRow();
 	if(selectRow != null && selectRow.getCellValue(saveDs.nameToIndex("kind")) == 'Folder'){
 		row.setCellValue(2, selectRow.getCellValue(saveDs.nameToIndex("id")));//have parent
 	}else{
 		row.setCellValue(2, null);
 	}
 	return row;
}

function $isNameExist()
{
	var saveDs = getDataset("savedQueryCondition");
	var saveName = getComponent("saveText").getValue();
	var nameIndex = saveDs.nameToIndex("display");
	var kindIndex = saveDs.nameToIndex("kind");
	for(var i=0,count=saveDs.getRowCount();i < count;i++){
		var temRow = saveDs.getRow(i);
		if(temRow.getCellValue(nameIndex) == saveName){
			if(doConditionSave.flag == temRow.getCellValue(kindIndex)){
				showMessageDialog("${ml:trans('yer_savefolderduplicate')}");
				return true;
			}
		}
	}
	return false;
}			
/**
 *删除收藏夹
 */
function deleteFavorit()
{
	var saveDs = getDataset("savedQueryCondition");
	var selectRow = saveDs.getSelectedRow();
	if(selectRow != null){
		var node = getComponent("savedTree").getNodeByRowId(selectRow.rowId);
		delNode.row = selectRow;
		if(node.hasChildren())
			ConfirmDialogComp.showDialog("其下有子结点，确定全部删除？", delNode, null, null, null);
		else
			ConfirmDialogComp.showDialog("确定删除？", delNode, null, null, null);
	}
}

/**
 *delImpl
 */
delNode = function()
{
	var row = delNode.row;
	var saveDs = getDataset("savedQueryCondition");
	saveDs.deleteRow(saveDs.getRowIndex(row));
	var page_fun_code = parent.getParameter("pageId");
	var nodeInfo = row.contentToXml() + "," + page_fun_code +","+getSessionAttribute("templateid"); //TODO
	getService("favorite_service").deleteQueryCondition(nodeInfo);
	delete( delNode.row );
}


/**
 *收藏夹树的叶子节点双击处理
 */
function templateTreeSaveConditionOndbclick(node)
{
	var rightDs;
	if(node.isLeaf){
		var row = node.nodeData;
		var page_fun_code = parent.getParameter("pageId");
		var nodeInfo = row.contentToXml() + "," + page_fun_code+","+getSessionAttribute("templateid");//TODO
		var conditionArr = getService("favorite_service").loadQueryCondition(nodeInfo);
		var arr = eval(conditionArr);

		var array = arr[0].split(",");
		var tabIndex = getComponent('sqlTab').getSelectedIndex();
		if(array[0] == '$AND_START' || array[4] == '$AND_START'){//advance tab
	    	if(tabIndex == 0)
	    		getComponent('sqlTab').activeTab(1);
		   	 rightDs = getDataset("queryConditionTreeDataset");
		   	 cleanDsData(false);
		}else{//common tab
			if(tabIndex == 1)
		    	getComponent('sqlTab').activeTab(0);
			rightDs = getDataset("queryConditionDataset");
			cleanDsData(true);
		}
		for(var i=0;i < arr.length;i++){
			var strArr = arr[i].split("#");
		    var addRow = new DatasetRow();
		    addRow.setCellValue(0, strArr[0]);//id
		    addRow.setCellValue(1, strArr[1]);//label
		    addRow.setCellValue(2, strArr[2]);//condition
		    addRow.setCellValue(3, strArr[3]);//condition_value
		    addRow.setCellValue(4, strArr[4]);//parentId
		    addRow.setCellValue(5, strArr[5]);//editorType
		    addRow.setCellValue(6, strArr[6]);//dataType
		    addRow.setCellValue(7, strArr[7]);//editorInfo
		    addRow.setCellValue(8, strArr[8]);//field
		    addRow.setCellValue(9, strArr[9]);//CondType
		    addRow.setCellValue(10, strArr[10]);//editorInfo2
		    addRow.setCellValue(11, strArr[11]);//tableName
		    addRow.setCellValue(12, strArr[12]);
		    addRow.setCellValue(13, strArr[13]);
		    addRow.setCellValue(14, strArr[14]);
		    rightDs.addRow(addRow);
		    rightDs.clearState();
		}
	}
}

/**
 *clean ds data
 */
function cleanDsData(flag)
{
	var rightDadaset;
	if(flag){//common
		rightDadaset = getDataset("queryConditionDataset");
		var count = rightDadaset.getRowCount();
		if(count != 0){
			for(var k=0; k < count; k++){
				rightDadaset.deleteRow(0);
			}
		}
	}
	else{//advance
		 rightDadaset = getDataset("queryConditionTreeDataset");
		 var count = rightDadaset.getRowCount();
		 if(count != 0){
			for(var k=0; k < count; k++){
				var tmpRow = rightDadaset.getRow(k);
				if(tmpRow.getCellValue(0) == '$AND_START'){
					rightDadaset.deleteRow(k);
					break;
				}
			}
		 }
	}
}
			
/**
 *设为默认查询条件
 */
function setDefaultCondition(item)
{
	var node = item.parentOwner.triggerObj;
 	var row = node.nodeData;
 	if(row.getCellValue(4) != 'Favorites')
 		return;
 	row.setCellValue(6,'Y');
 	$saveFavoritesImpl(row);
 		
 		//other row set not default
 	var saveDs = getDataset("savedQueryCondition");
 	for(var i=0; i < saveDs.getRowCount(); i++){
 		var temRow = saveDs.getRow(i);
 		if($isTrue( row.getCellValue(6)) && row.getCellValue(4)=='Favorites'){
 			row.setCellValue(6,'N');
 			$saveFavoritesImpl(row);
 		}
 	}
}

/**
 *string to boolean
 */

function $isTrue(val){
	if (val != null && val.length > 0 && (val == "true" || val.charAt(0) == 'Y' || val.charAt(0) == 'y'))
		return true;
    else
    	return false;
}
		    
/**
 *modify name
 */
 function modifySavedConditionName(item)
 {
 	var node = item.parentOwner.triggerObj;
	var row = node.nodeData;
	var dialog = getComponent("saveDialog");
	dialog.show();
	doConditionSave.flag = 'modify';
	doConditionSave.row = row;
 }
/**
 *具体的保存方法
 *@param  节点信息row
 *@return 节点对应的条件row数组 
 */
function $saveFavoritesImpl(row)
{
	var saveDs = getDataset("savedQueryCondition");
	var conditionDs = null;
	if(getComponent('sqlTab').getSelectedIndex() == 0)
		conditionDs = getDataset("queryConditionDataset");
	else
		conditionDs = getDataset("queryConditionTreeDataset");
	var rows = conditionDs.getRows();
	var returnArrayRow;
	if(rows != null && rows.length > 0){
		var condArr = new Array;
		for(var i = 0; i < rows.length; i ++){
			var content = rows[i].contentToXml();
			condArr.push(content);
		}
	 	var page_fun_code = getParameter("otherPageId");
		var nodeInfo = row.contentToXml() + "," + page_fun_code + "," + getSessionAttribute("templateid");//TODO
		getService("favorite_service").saveQueryCondition(condArr, nodeInfo);
		returnArrayRow = new Array();
		returnArrayRow.push(row);
	 }else{
	 	showMessageDialog("条件不能为空!");
		return;
	 }
	 return returnArrayRow; 
}
		    
function toDeleteTreeCondition(item) 
{
    if(item != null)
     {
        var node = item.parentOwner.triggerObj;
        $templateAdanceTreeRowDelete(node.nodeData);
     }
 }
 
 function $templateAdanceTreeRowDelete(row){
	 var ds = getDataset("queryConditionTreeDataset");
	 var count = ds.getRowCount();
	 //ds.deleteRow(ds.getRowIndex(row));
	 if(count != 0){
		for(var k=0; k < count; k++){
			var tmpRow = ds.getRow(k);
			if(tmpRow.getCellValue(0) == '$AND_START'){
				ds.deleteRow(k);
				break;
			}
		}
	 }
 }